

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;

public class UploadRunnableImpl implements Runnable {
    private Socket socket;

    public UploadRunnableImpl(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            String fileName = dataInputStream.readUTF();
            long fileLength = dataInputStream.readLong();
            System.out.println("服务端接收上传文件："+fileName+"，其大小为："+fileLength);
            String fileDir = "E:";
            String filePath= fileDir + File.separator+fileName;

            FileOutputStream fileOutputStream = new FileOutputStream(filePath);

            byte[] buf = new byte[1024*1];
            int len = 0;
            while ((len = dataInputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, len);
            }
            fileOutputStream.flush();
            fileOutputStream.close();
            dataInputStream.close();
            socket.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }

}

